﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OurFAS.Data;

namespace OurFAS.MyAssetManage
{
    public partial class FrmBorrow : Form
    {
        private string m_sCode = null;
        private string m_sDataSource = null;

        public FrmBorrow(string sCode, string sDataSource)
        {
            InitializeComponent();

            m_sCode = sCode;
            m_sDataSource = sDataSource;

            InitControl(sCode, sDataSource);
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbBorrowMan.Text.Trim()))
            {
                MessageBox.Show("借用人员不能为空，请填写！");
                tbBorrowMan.BackColor = Color.Yellow;
                return;
            }

            OurFasDBEntities entity = new OurFasDBEntities();
            Tbl_EvtBorrow borrow = new Tbl_EvtBorrow();
            borrow.EventCode = string.Format("JY{0:yyyyMMddhhmmss}", DateTime.Now);
            borrow.AssetCode = GetDataFromTextbox(tbAssetCode);
            borrow.AssetName = GetDataFromTextbox(tbAssetName);
            borrow.AssetSpec = GetDataFromTextbox(tbAssetSpec);
            borrow.BorrowDate = DataHelper.GetDateToString(dtpBorrowDate.Value);
            borrow.BorrowMan = GetDataFromTextbox(tbBorrowMan);
            borrow.PlanReturnDate =DataHelper.GetDateToString(dtpPlanReturnDate.Value);
            borrow.ReturnDate = "null";
            borrow.IsReturn = false;
            borrow.Remark = GetDataFromTextbox(tbRemark);
            entity.Tbl_EvtBorrow.AddObject(borrow);
            try
            {
                entity.SaveChanges();
                MessageBox.Show("已完成编号为 " + tbAssetCode.Text.Trim() + " 的设备的借用！");
            }
            catch
            {
                MessageBox.Show("提交设备借用记录出错，请重试或联系管理员！");
                return;
            }
        }

        #region 局部方法
        //初始化控件
        private void InitControl(string code, string datasource)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                tbAssetCode.Text = "";
                tbAssetName.Text = "";
                tbAssetSpec.Text = "";
                tbDept.Text = "";
                tbIncharge.Text = "";
                tbRemark.Text = "";
                tbBorrowMan.Text = "";
                return;
            }

            switch (datasource)
            {
                case "base":
                    Tbl_BaseAssets baserecord = AssetsHelper.GetBaseAssetByCode(code);
                    if (baserecord == null)
                    {
                        tbAssetCode.Text = "";
                        tbAssetName.Text = "";
                        tbAssetSpec.Text = "";
                        tbDept.Text = "";
                        tbIncharge.Text = "";
                        tbRemark.Text = "";
                        tbBorrowMan.Text = "";
                    }
                    else
                    {
                        tbAssetCode.Text = code;
                        tbAssetName.Text = baserecord.AssetName.Trim();
                        tbAssetSpec.Text = baserecord.AssetSpec.Trim();
                        tbDept.Text = baserecord.Dept.Trim();
                        tbIncharge.Text = baserecord.InCharge.Trim();
                        tbRemark.Text = "";
                        tbBorrowMan.Text = "";
                    }
                    break;
                case "self":
                    Tbl_SelfAssets selfrecord = AssetsHelper.GetSelfAssetByCode(code);
                    if (selfrecord == null)
                    {
                        tbAssetCode.Text = "";
                        tbAssetName.Text = "";
                        tbAssetSpec.Text = "";
                        tbDept.Text = "";
                        tbIncharge.Text = "";
                        tbRemark.Text = "";
                        tbBorrowMan.Text = "";
                    }
                    else
                    {
                        tbAssetCode.Text = code;
                        tbAssetName.Text = selfrecord.AssetName.Trim();
                        tbAssetSpec.Text = selfrecord.AssetSpec.Trim();
                        tbDept.Text = selfrecord.Dept.Trim();
                        tbIncharge.Text = selfrecord.InCharge.Trim();
                        tbRemark.Text = "";
                        tbBorrowMan.Text = "";
                    }
                    break;
                case "measure":
                    Tbl_MeasureAssets measurerecord = AssetsHelper.GetMeasureAssetByCode(code);
                    if (measurerecord == null)
                    {
                        tbAssetCode.Text = "";
                        tbAssetName.Text = "";
                        tbAssetSpec.Text = "";
                        tbDept.Text = "";
                        tbIncharge.Text = "";
                        tbRemark.Text = "";
                        tbBorrowMan.Text = "";
                    }
                    else
                    {
                        tbAssetCode.Text = code;
                        tbAssetName.Text = measurerecord.AssetName.Trim();
                        tbAssetSpec.Text = measurerecord.AssetSpec.Trim();
                        tbDept.Text = measurerecord.Dept.Trim();
                        tbIncharge.Text = measurerecord.InCharge.Trim();
                        tbRemark.Text = "";
                        tbBorrowMan.Text = "";
                    }
                    break;
                default:
                    break;
            }
        }

        //从文本框控件中读取信息
        private string GetDataFromTextbox(TextBox tb)
        {
            string result = "";
            if (tb == null)
            {
                return result;
            }
            else if (tb.Text.Trim() == "")
            {
                return result;
            }
            else
            {
                result = tb.Text.Trim();
                return result;
            }
        }
        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> reportDict = new Dictionary<string, string>();

            reportDict.Add("AssetCode", tbAssetCode.Text);
            reportDict.Add("AssetName", tbAssetName.Text);
            reportDict.Add("AssetSpec", tbAssetSpec.Text);
            reportDict.Add("BorrowOutDepart", tbDept.Text);
            reportDict.Add("BorrowOutMan", tbIncharge.Text);
            reportDict.Add("BorrowMan", tbBorrowMan.Text);
            reportDict.Add("Remark", tbRemark.Text);

            MyReport.ReportPrintForm window = new MyReport.ReportPrintForm(StringHelper.sReprotBorrow, reportDict);
            window.Show();
        }

        private void FrmBorrow_Load(object sender, EventArgs e)
        {

        }
    }
}
